package com.younion.action.manage;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.younion.business.ICardEntityService;
import com.younion.business.ICardLevelService;
import com.younion.business.IUpgradeRuleService;
import com.younion.common.base.BaseAction;
import com.younion.vo.CardEntityVO;
import com.younion.vo.CardLevelVO;
import com.younion.vo.UpgradeRuleVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author wells
 */
public class UpgradeRuleAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory
			.getLog(UpgradeRuleAction.class);

	private IUpgradeRuleService upgradeRuleService = null;

	private UpgradeRuleVO upgradeRuleVO = new UpgradeRuleVO();

	private List<UpgradeRuleVO> upgradeRuleList;//结果集列表

	private List<CardEntityVO> cardEntityVOList;
	
	private CardEntityVO oldCardEntityVO;
	private UpgradeRuleVO oldUpgradeRuleVO;

	private ICardLevelService cardLevelService = null;
	@Autowired
	private ICardEntityService cardEntityService = null;

	/**
	 *列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	public String listUpgradeRule() {
		try {
			String cardEntityId = request.getParameter("cardEntityId");
			String customerId = request.getParameter("customerId");
			oldUpgradeRuleVO = upgradeRuleService.getByOldCardId(new Integer(cardEntityId));
			Map<String, Object> cardParamMap = new HashMap<String, Object>();
			cardParamMap.put("customerId", customerId);
			cardEntityVOList = (List<CardEntityVO>) cardEntityService.getByMap(cardParamMap);
			if(cardEntityVOList!=null&&!cardEntityVOList.isEmpty()){
				for(CardEntityVO cardEntityVO:cardEntityVOList){
					if(cardEntityVO.getId().equals(new Integer(cardEntityId))){
						oldCardEntityVO = cardEntityVO;
					}
				}
				cardEntityVOList.remove(oldCardEntityVO);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	public String addUpgradeRule() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度JSP
	 */
	public String editUpgradeRule() {
		try {
			upgradeRuleVO = upgradeRuleService.getById(upgradeRuleVO.getId()
					.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存
	 */
	public String saveUpgradeRule() {
		try {
			upgradeRuleVO = upgradeRuleService.save(upgradeRuleVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	public String deleteUpgradeRule() {
		try {
			String pkId = request.getParameter("key");
			upgradeRuleService.remove(Arrays.asList(pkId.split(",")));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 查看鿴
	 */
	public String viewUpgradeRule() {
		try {
			String pkId = request.getParameter("key"); 
			upgradeRuleVO = upgradeRuleService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public UpgradeRuleVO getUpgradeRuleVO() {
		return upgradeRuleVO;
	}

	public void setUpgradeRuleVO(UpgradeRuleVO upgradeRuleVO) {
		this.upgradeRuleVO = upgradeRuleVO;
	}

	public void setUpgradeRuleService(IUpgradeRuleService upgradeRuleService) {
		this.upgradeRuleService = upgradeRuleService;
	}

	public List<UpgradeRuleVO> getUpgradeRuleList() {
		return upgradeRuleList;
	}

	public void setUpgradeRuleList(List<UpgradeRuleVO> upgradeRuleList) {
		this.upgradeRuleList = upgradeRuleList;
	}

	public ICardLevelService getCardLevelService() {
		return cardLevelService;
	}

	public void setCardLevelService(ICardLevelService cardLevelService) {
		this.cardLevelService = cardLevelService;
	}

	public void setCardEntityService(ICardEntityService cardEntityService) {
		this.cardEntityService = cardEntityService;
	}

	public List<CardEntityVO> getCardEntityVOList() {
		return cardEntityVOList;
	}

	public void setCardEntityVOList(List<CardEntityVO> cardEntityVOList) {
		this.cardEntityVOList = cardEntityVOList;
	}

	public CardEntityVO getOldCardEntityVO() {
		return oldCardEntityVO;
	}

	public void setOldCardEntityVO(CardEntityVO oldCardEntityVO) {
		this.oldCardEntityVO = oldCardEntityVO;
	}

	public UpgradeRuleVO getOldUpgradeRuleVO() {
		return oldUpgradeRuleVO;
	}

	public void setOldUpgradeRuleVO(UpgradeRuleVO oldUpgradeRuleVO) {
		this.oldUpgradeRuleVO = oldUpgradeRuleVO;
	}
}